// ==UserScript==
// @name         在新标签页中打开链接但不激活
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://tieba.baidu.com/*
// @match        https://www.bilibili.com/*
// @require      http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.3.js
// @require      https://lib.baomitu.com/zepto/latest/zepto.min.js
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant           GM_info
// @grant           GM_openInTab
// ==/UserScript==

(function() {
    'use strict';
    const DOC = document;
    const openInTab = (url, active = false, options = {}) => {
        if (url) return GM_openInTab(url, { active: !!active, setParent: true, ...options });
    };

    class Common {
        constructor() {
            this.globalClick();
        }
        // G_CLICK
        globalClick = () => {
            const selectors = ['a[href]'];
            if (!selectors?.length) return;
            const getTarget = ({ target }) => target.closest(selectors) || false;

            DOC.addEventListener("click", e => {
                const closest = getTarget(e);
                if (!closest) return;
                console.log(closest);

                let url = "";
                if (closest.href) {
                    url = closest.href;
                }
                if (!url) return;

                e.preventDefault();
                // 也许会有情景影响到，后续看看
                // e.stopPropagation();
            });

            let _event;
            DOC.addEventListener("mousedown", e => {
                if (e.button !== 0) return;

                const target = getTarget(e);
                if (!target) return;

                e.preventDefault();
                target.oncontextmenu = e => e.preventDefault();
                _event = e;
            });
            DOC.addEventListener("mouseup", e => {
                if (e.button !== 0 || !_event) return;

                const target = getTarget(e);
                if (!target) return;
                console.log(target);
                e.preventDefault();
                openInTab(target.href);
            });
        };
    }
    try {
        new Common();
    } catch (err) {
        console.error(`${GM_info.script.name}: 无匹配模块`);
    }
})();